package com.fingard.dsp.bank.directbank.egbank01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class EGBANK01SinglePay extends EGBANK01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        String sendContent;
        EGBANK01Header tmpHeader = new EGBANK01Header();
        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            sendContent=d_190802_SameBank(p_req1908,tmpHeader);
        } else {
            sendContent=d_190802_DiffBank(p_req1908,tmpHeader);
        }

        tmpRetDetail = d_190802_SglPay(p_req1908, sendContent, tmpHeader);
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    private String d_190802_SameBank(ReqSingle p_req1908, EGBANK01Header p_bankHeader) throws Exception {
        p_bankHeader.needSigned = true;
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.opName = "submitStepPayOp";
        p_bankHeader.signStrList = new String[]{"serialNo", "cstNo", "reqTime", "erpPid", "ACNO", "OPAC", "OPNM", "TRAM", "USAG", "REMK"};

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, p_bankHeader);
        xmlWriter.writeElementString("cstNo", bankFront.loginName);
        XElement tmpXElement = new XElement("reqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("OPNM", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.memo));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_190802_DiffBank(ReqSingle p_req1908, EGBANK01Header p_bankHeader) throws Exception {
        p_bankHeader.needSigned = true;
        p_bankHeader.serialNo = p_req1908.singleDetail.askNumber;
        p_bankHeader.opName = "outInCityPayOp";
        p_bankHeader.signStrList = new String[]{"serialNo", "cstNo", "reqTime", "erpPid", "ACNO", "OPAC", "OPNM", "EXNM", "PBNO", "TRAM", "USAG", "IFFT", "REMK"};

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, p_bankHeader);
        xmlWriter.writeElementString("cstNo", bankFront.loginName);
        XElement tmpXElement = new XElement("reqParam", "");
        tmpXElement.addChild(new XElement("ACNO", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("OPAC", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("OPNM", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("EXNM", p_req1908.singleDetail.cnapsName));
        tmpXElement.addChild(new XElement("PBNO", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("TRAM", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("USAG", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("IFFT", p_req1908.singleDetail.paySpd));
        tmpXElement.addChild(new XElement("REMK", p_req1908.singleDetail.memo));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private RetAtsSglDetail d_190802_SglPay(ReqSingle p_req1908, String p_sendContent, EGBANK01Header p_bankHeader) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();

        String[] tmpStrRet = sendToBank(p_sendContent, p_bankHeader);
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmprspcod = tmpBkReader.getFirstText("/HFBankData/opRep/retCode");
                String tmprspmsg = tmpBkReader.getFirstText("/HFBankData/opRep/errMsg");
                if (tmprspcod != null && tmprspcod.equals("0")) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmprspmsg;
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    retDetail.transState = "000000";
                    retDetail.payInfoCode = tmprspcod;
                    retDetail.outTransID = tmpBkReader.getFirstText("/HFBankData/opRep/opResult/ZJSN");
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmprspmsg;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }


    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        EGBANK01Header bankHeader = new EGBANK01Header();
        bankHeader.opName = "qryStepPayOp";
        bankHeader.serialNo = getQryTransID();
        bankHeader.needSigned = false;

        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeElementString("cstNo", bankFront.loginName);
        xmlWriter.writeStartElement("reqParam");
        xmlWriter.writeElementClosed("beginDate");
        xmlWriter.writeElementClosed("endDate");
        xmlWriter.writeElementString("OSNO", p_req1944.askNumber);
        xmlWriter.writeElementClosed("orderState");
        xmlWriter.writeElementString("turnPageBeginPos", "1");
        xmlWriter.writeElementString("turnPageShowNum", "50");
        xmlWriter.writeEndDocument();

        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/HFBankData/opRep/retCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/HFBankData/opRep/errMsg");
                if (tmpRetCode != null && tmpRetCode.equals("0")) {
                    if (tmpBkReader.read("/HFBankData/opRep/opResultSet/opResult")) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.readedXElement.getSingleText("BKIF");
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.transState = "0000" + tmpBkReader.readedXElement.getSingleText("REST");
                        retDetail.payInfoCode = "0000" + tmpBkReader.readedXElement.getSingleText("REST");
                        retDetail.transAmt = tmpBkReader.readedXElement.getSingleText("TRAM");
                        retDetail.outTransID = tmpBkReader.readedXElement.getSingleText("PIXT");
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = "该次查询银行暂无交易结果返回！";
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpRetInfo;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1944.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
